/**
 * QuestionService.java
 * Version 1.0.0
 * Created on 2018年7月16日
 *
 */
package com.yincheng.oes.business.base.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yincheng.oes.business.base.entity.KnowledgeOutline;
import com.yincheng.oes.business.base.entity.Question;
import com.yincheng.oes.business.base.entity.QuestionForm;
import com.yincheng.oes.business.base.entity.QuestionKnowledgeOutline;
import com.yincheng.oes.business.base.mapper.KnowledgeOutlineMapper;
import com.yincheng.oes.business.base.mapper.QuestionKnowledgeOutlineMapper;
import com.yincheng.oes.business.base.mapper.QuestionMapper;

/**
 * @author yangsy
 * @version 1.0.0
 * @since
 * 创建时间：2018年7月16日
 * 功能描述：题目信息服务类
 * 邮箱地址：446948434@qq.com
 */
@Service
public class QuestionService {

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private KnowledgeOutlineMapper knowledgeOutlineMapper;

    @Autowired
    private QuestionKnowledgeOutlineMapper questionKnowledgeOutlineMapper;

    /**
     * 根据题目id获取题目信息
     *
     * @param id
     * @return
     */
    public Question getQuestionById(Integer id) {
        Question question = questionMapper.selectById(id);
        if (question == null) {
            question = new Question();
            question.setId(id);
            question.setCreateTime(new Date());
            question.setValid("1");
            return question;
        }
        return question;
    }

    /**
     * 根据题目id获取相关联的题目列表
     *
     * @param id
     * @return
     */
    public List<Question> getRelatedQuestionById(Integer id) {
        List<Question> list = questionMapper.selectRelatedById(id);
        return list;
    }

    public QuestionForm getQuestionFormById(Integer id) {
        QuestionForm questionForm = new QuestionForm();
        Question question = getQuestionById(id);
        List<KnowledgeOutline> outlines = knowledgeOutlineMapper.selectByQuesId(id);
        questionForm.setQuestion(question);
        questionForm.setOutlines(outlines);
        return questionForm;
    }

    /**
     * 新建题目信息
     *
     * @param form
     */
    @Transactional
    @Deprecated
    public void createQuestion(QuestionForm form) {
        List<QuestionKnowledgeOutline> questionOutlines = new ArrayList<QuestionKnowledgeOutline>();

        Question question = form.getQuestion();
        Integer id = questionMapper.selectMaxId();
        question.setId(id);
        question.setCreateTime(new Date());
        question.setValid("1");
        questionMapper.insert(question);// 创建题目信息

        List<KnowledgeOutline> outlines = form.getOutlines();
        outlines.forEach(e -> {
            QuestionKnowledgeOutline questionOutline = new QuestionKnowledgeOutline();
            questionOutline.setOutlineId(e.getId());
            questionOutline.setQuesId(id);
            questionOutlines.add(questionOutline);
        });

        questionOutlines.forEach(e -> questionKnowledgeOutlineMapper.insert(e));// 保存题目和大纲关联关系
    }

    public Question createQuestion() {
        Question question = new Question();
        Integer id = questionMapper.selectMaxId();
        question.setId(id);
        question.setCreateTime(new Date());
        question.setValid("1");
        return question;
    }

    /**
     * 保存题目信息
     *
     * @param form
     */
    @Transactional
    public void saveQuestion(QuestionForm form) {
        List<QuestionKnowledgeOutline> questionOutlines = new ArrayList<QuestionKnowledgeOutline>();

        Question question = form.getQuestion();
        int i = questionMapper.update(question);// 更新题目信息
        if (i == 0) {
            if ("2".equals(question.getQuesSource())) {
                Integer num = questionMapper.countAsks();
                String quesCode = getSerialNum(num);
                question.setQuesCode(quesCode);
            }
            question.setCreateTime(new Date());
            question.setValid("1");
            questionMapper.insert(question);
        }

        Integer id = question.getId();
        List<KnowledgeOutline> outlines = form.getOutlines();
        outlines.forEach(e -> {
            QuestionKnowledgeOutline questionOutline = new QuestionKnowledgeOutline();
            questionOutline.setOutlineId(e.getId());
            questionOutline.setQuesId(id);
            questionOutlines.add(questionOutline);
        });
        questionKnowledgeOutlineMapper.deleteByQuesId(id);// 删除之前的题目和大纲关系
        questionOutlines.forEach(e -> questionKnowledgeOutlineMapper.insert(e));// 保存题目和大纲关联关系
    }

    private String getSerialNum(Integer num) {
        String format = String.format("%06d", num);
        StringBuffer sb = new StringBuffer("TW").append(format);
        return sb.toString();
    }
}
